package cn.wyl.pet.service.impl;

import cn.wyl.basic.exception.BusinessException;
import cn.wyl.basic.util.BaiduAuditUtils;
import cn.wyl.pet.domain.Pet;
import cn.wyl.pet.domain.PetDetail;
import cn.wyl.pet.mapper.PetDetailMapper;
import cn.wyl.pet.mapper.PetMapper;
import cn.wyl.pet.service.IPetService;
import cn.wyl.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.security.auth.login.LoginContext;
import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    PetMapper petMapper;

    @Autowired
    PetDetailMapper petDetailMapper;

    @Transactional
    @Override
    public void update(Pet pet) {

        petMapper.update(pet);
        PetDetail petDetail = pet.getPetDetail();
        petDetail.setPetId(pet.getId());
        petDetailMapper.update(petDetail);
    }


    @Transactional
    @Override
    public void save(Pet pet) {
        petMapper.save(pet);
        PetDetail petDetail = pet.getPetDetail();
        petDetail.setPetId(pet.getId());
        petDetailMapper.save(petDetail);

    }
    @Transactional
    @Override
    public void remove(Long id){
        petMapper.remove(id);
        petDetailMapper.deleteByPetId(id);
    }

    @Transactional
    @Override
    public void patchDel(List<Long> ids) {

        for(Long id : ids){
            remove(id);
        }
    }

    @Override
    public void offSale(List<Long> ids) {
        petMapper.offSale(ids,new Date());

    }

    @Override
    public String onSale(List<Long> ids) {
        StringBuilder failAuditPets=new StringBuilder("");
        for(Long id:ids){
            Pet pet = petMapper.loadById(id);
            String auditText = pet.getName();
            String auditImages = pet.getResources();

            Boolean textBoolean=true;
            if (auditText.trim()!=""&&auditText!=null){
                textBoolean = BaiduAuditUtils.TextCensor(auditText);
            }
            Boolean ImgsBoolean = BaiduAuditUtils.ImgsCensor(auditImages);
            if (textBoolean&&ImgsBoolean){
                // 数据库操作pet表更新上架状态，上架时间
                petMapper.onSale(id,new Date());

                //记录审核日志
              /*  PetOnlineAuditLog auditLog = new PetOnlineAuditLog();
                auditLog.setState(1);
                auditLog.setPetId(id);
                //暂时写死
                auditLog.setAuditId(326l);
                auditLog.setNote("审核成功！");
                petOnlineAuditLogMapper.save(auditLog);*/
            }else{
                //审核失败-note-当前是下架状态，如果审核失败！
                //记录审核日志
                /*PetOnlineAuditLog auditLog = new PetOnlineAuditLog();
                auditLog.setState(0);
                auditLog.setPetId(id);
                auditLog.setAuditId(326l);
                auditLog.setNote("审核失败,宠物名称或图片不合法!!!");
                petOnlineAuditLogMapper.save(auditLog);*/
                failAuditPets= failAuditPets.append(id+"号"+auditText+",");

            }

        }
        return failAuditPets.toString();
    }
}
